package com.vanzo.encryption;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(new Pdt().stringFromJNI());
//        byte[] inValue = Pdt.hexStringToByteArray("Hello World!");
//        byte[] outValue = new byte[Pdt.MAX_BUFFER_LEN];
//        Integer oLen = new Integer(0);
//        int ret = Pdt.systemCommand(inValue, outValue, oLen);
//        Log.w(TAG, "oLen = " + oLen);
//        Log.w(TAG, "ret = " + ret);

//        Integer groudId = new Integer(0);
//        Integer terminalId = new Integer(0);
//        Integer totalTerminalSize = new Integer(5);
//        byte[] outValue = new byte[Pdt.MAX_BUFFER_LEN];
        byte[] gk = new byte[Pdt.MAX_BUFFER_LEN];
//        byte[] kk = new byte[Pdt.MAX_BUFFER_LEN];
        Integer ogkLen = new Integer(1024);
        int gkRet = Pdt.getGroupKey(gk, ogkLen);
        Log.w(TAG, "ret = " + gkRet);
        Log.w(TAG, "gk = " + bytesToHexString(gk));

        byte[] kk = new byte[8];
//        byte[] kk = new byte[Pdt.MAX_BUFFER_LEN];
        Integer okkLen = new Integer(8);
        int kkRet = Pdt.getKillKey(kk, okkLen);
        Log.w(TAG, "ret = " + kkRet);
        Log.w(TAG, "kk = " + bytesToHexString(kk));



        byte[] nfc = new byte[Pdt.MAX_BUFFER_LEN * 4];
        Integer oNfcLen = new Integer(Pdt.MAX_BUFFER_LEN * 4);
        int nfcRet = Pdt.getSuperkey2NFC(3, 4,5, 1, gk, kk, nfc, oNfcLen);
        Log.w(TAG, "oNfcLen = " + oNfcLen);
        Log.w(TAG, "nfc = " + bytesToHexString(nfc));



        byte[] binary = new byte[Pdt.MAX_BUFFER_LEN * 2];
        Integer oBinaryLen = new Integer(Pdt.MAX_BUFFER_LEN * 2);
        Integer groudId = new Integer(0);
        Integer terminalId = new Integer(0);
        Integer totalTerminalSize = new Integer(0);
        Integer firstTid = new Integer(0);

        int binaryRet = Pdt.parseSuperkeyFromNFC(nfc, oNfcLen, groudId, terminalId, totalTerminalSize, firstTid, binary, oBinaryLen);
        Log.w(TAG, "ret = " + binaryRet);
        Log.w(TAG, "groudId = " + groudId);
        Log.w(TAG, "terminalId = " + terminalId);
        Log.w(TAG, "totalTerminalSize = " + totalTerminalSize);
        Log.w(TAG, "firstTid = " + firstTid);
        Log.w(TAG, "binary = " + bytesToHexString(binary));
        Log.w(TAG, "oBinaryLen = " + oBinaryLen);
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
}
